
var vm = new Vue({
	el: '#app',
	
	data:{
		arLetter:["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],
		queList: [],	//预览试题
		TYPE: '',		//试题类型
		LEVEL: '',		//试题级别
		QTYPE: '',		//试题题型
		QID: '',		//试题ID
		MYANSWER:'',	//我的答案
		CBANSWER:'',	//多选答案
		DTANSWER:'',	//大题答案
		YANSWER:'',		//正确答案
		serverurl: '',	//后台地址
		loading:false	//加载状态
    },
    
	methods: {
		
        //初始执行
        init() {
        	this.serverurl = httpurl;
        	this.QTYPE = this.getUrlKey('QTYPE');
    		this.getDic();
        },
        
		//获取试题ID
        getTestQuestions: function () {
			//随机获取试题
        	$.ajax({
        		xhrFields: {
                    withCredentials: true
                },
        		type: "POST",
        		url: httpurl+'testpapermx/automatic',
        		data: {QNUMBER:1,TYPE:this.TYPE,LEVEL:this.LEVEL,TABN:this.QTYPE,tm:new Date().getTime()},//TYPE:this.TYPE选题的题类别(不是题型)，TABN：this.QTYPE 是题型(用来根据此参数区分表名来读取数据)
        		dataType:"json",
        		success: function(data){
        		 if("success" == data.result){
        			var fid = data.fid;	//试题的ID
     	   			if("" != fid){
     	   				vm.viewQue(fid);
     	   				$("#isnext").show();
     	   				$("#fhAnswer").hide();
     	   				vm.MYANSWER = '';
     	   				vm.DTANSWER = '';
     	   			 }else{
     	   				$("#FHAU").tips({
	     	   				side:1,
	     	   	            msg:"没有符合要求的试题",
	     	   	            bg:'#AE81FF',
	     	   	            time:3
	     	   	        });
     	   			 }
        		 }else if ("exception" == data.result){
                 	showException("随机获取试题",data.exception);//显示异常
                 }
        		}
        	}).done().fail(function(){
                swal("登录失效!", "请求服务器无响应，稍后再试", "warning");
            });
        },
        
      	//显示题目
        viewQue: function (fid) {
        	var qurl = "";
			switch(this.QTYPE){
			    case 'A':
			    	qurl = 'singleelection/getListByIDS';
			        break;
			    case 'B':
			    	qurl = 'multipleselection/getListByIDS';
			        break;
			    case 'C':
			    	qurl = 'judgementquestion/getListByIDS';
			        break;
			    case 'D':
			    	qurl = 'completion/getListByIDS';
			        break;
			    case 'E':
			    	qurl = 'largequestion/getListByIDS';
			        break;
			    case 'F':
			    	qurl = 'compoundquestion/getListByIDS';
			        break;
			}
	        //预览试题列表
        	$.ajax({
        		xhrFields: {
                    withCredentials: true
                },
        		type: "POST",
        		url: httpurl+qurl,
        		data: {DATA_IDS:fid,tm:new Date().getTime()},
        		dataType:"json",
        		success: function(data){
        		 if("success" == data.result){
        			 vm.queList = data.varList;
        		 }else if ("exception" == data.result){
                 	showException("试题预览",data.exception);//显示异常
                 }
        		}
        	}).done().fail(function(){
                swal("登录失效!", "请求服务器无响应，稍后再试", "warning");
            });
        },
        
    	//单选多选题判断题的选项格式处理
    	formatOption: function (arField,br,fi1,fi2,type,fa,qid) {
    		var str = "";
    		if(type == 'C'){
    			str += '<div style="font-size:15px;" class="radio radio-primary d-inline"><input onclick="selection1(\'true\',\''+fa+'\',\''+qid+'\',\'f'+fi1+fi2+'\')" type="radio" name="radio-p-'+fi1+fi2+'" id="f'+fi1+fi2+'1"><label for="f'+fi1+fi2+'1" class="cr">正确</label></div>   ';
    			str += '<div style="font-size:15px;" class="radio radio-primary d-inline"><input onclick="selection1(\'false\',\''+fa+'\',\''+qid+'\',\'f'+fi1+fi2+'\')" type="radio" name="radio-p-'+fi1+fi2+'" id="f'+fi1+fi2+'2"><label for="f'+fi1+fi2+'2" class="cr">错误</label></div>';
    		}else{
    			for(var i=0;i<arField.length;i++){
        			if(type == 'A'){
        				str += '<div style="font-size:15px;" class="radio radio-primary d-inline"><input onclick="selection1(\''+this.arLetter[i]+'\',\''+fa+'\',\''+qid+'\',\'f'+fi1+fi2+'\')" type="radio" name="radio-p-'+fi1+fi2+'" id="f'+fi1+fi2+i+'"><label for="f'+fi1+fi2+i+'" class="cr"></label></div>'+arField[i]+'&nbsp;&nbsp;&nbsp;&nbsp;'+br;
        			}else{
        				str += '<div style="font-size:15px;" class="checkbox checkbox-primary checkbox-fill d-inline"><input onclick="selection2(\''+this.arLetter[i]+'\',\'f'+fi1+fi2+i+'\',\'f'+fi1+fi2+'\',\''+fa+'\',\''+qid+'\')" type="checkbox" name="radio-p-'+fi1+fi2+'" id="f'+fi1+fi2+i+'"><label for="f'+fi1+fi2+i+'" class="cr"></label></div>'+arField[i]+'&nbsp;&nbsp;&nbsp;&nbsp;'+br;
        			}
        		}
    		}
    		return str;
        },
        
    	compileStr: function (code){
            var c=String.fromCharCode(code.charCodeAt(0)+code.length);  
            for(var i=1;i<code.length;i++){        
                c+=String.fromCharCode(code.charCodeAt(i)+code.charCodeAt(i-1));  
            }     
            return escape(c);
        },
        
        //显示答案
        showAnswer: function (){
        	if(this.QTYPE == 'A' || this.QTYPE == 'B' || this.QTYPE == 'C'){
        		if(vm.MYANSWER == ''){
        			$("#NNTT").tips({
     	   				side:2,
     	   	            msg:"未作答",
     	   	            bg:'#AE81FF',
     	   	            time:3
     	   	        });
        		}else if(vm.MYANSWER == vm.YANSWER){
        			$("#NNTT").tips({
     	   				side:2,
     	   	            msg:"恭喜, 答对了",
     	   	            bg:'#83E218',
     	   	            time:5
     	   	        });
            	}else{
            		$("#NNTT").tips({
     	   				side:2,
     	   	            msg:"很遗憾, 答错了",
     	   	            bg:'#FF0000',
     	   	            time:5
     	   	        });
            	}
        	}
        	$("#fhAnswer").show();
        },
    	
    	//调用数据字典(试题类型和试题级别)
    	getDic: function(){
    		$.ajax({
    			xhrFields: {
                    withCredentials: true
                },
    			type: "POST",
    			url: httpurl+'dictionaries/getLevels',
    	    	data: {DICTIONARIES_ID:'testquestionstype',tm:new Date().getTime()},//试题类型
    			dataType:'json',
    			success: function(data){
    				$("#TYPE").html('<option value="" >试题类型不限</option>');
    				 $.each(data.list, function(i, dvar){
    					 $("#TYPE").append("<option value="+dvar.BIANMA+">"+dvar.NAME+"</option>");
    				 });
    			}
    		});
    		$.ajax({
    			xhrFields: {
                    withCredentials: true
                },
    			type: "POST",
    			url: httpurl+'dictionaries/getLevels',
    	    	data: {DICTIONARIES_ID:'testquestionslevel',tm:new Date().getTime()},//试题级别
    			dataType:'json',
    			success: function(data){
    				$("#LEVEL").html('<option value="" >试题级别不限</option>');
    				 $.each(data.list, function(i, dvar){
    					 $("#LEVEL").append("<option value="+dvar.BIANMA+">"+dvar.NAME+"</option>");
    				 });
    			}
    		});
    	},
    	
    	//单选多选题的选项换行
    	lineFeed: function (arField) {
    		var str = "";
    		for(var i=0;i<arField.length;i++){
    			str += arField[i] + '<br>';
    		}
    		return str;
        },
    	
    	//根据url参数名称获取参数值
        getUrlKey: function (name) {
            return decodeURIComponent(
                (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
        }
		
	},
	
	mounted(){
        this.init();
    }
})

//单选和判断选中
var hcid="fh";
function selection1(value,fa,qid,id2){
	vm.MYANSWER = value;
}

//复选选中
function selection2(value,id,id2,fa,qid){
	if($('#'+id).attr('checked')){
		vm.CBANSWER = vm.CBANSWER + value + ',fh,';
	}else{
		vm.CBANSWER = vm.CBANSWER.replace(value+',fh,','');
	}
	var arrANSWER = vm.CBANSWER.split(',fh,');
	var stra = (arrANSWER.sort());
	if("" != stra.join('')){
		vm.MYANSWER = stra.join('');
	}
}